# -*- coding: utf-8 -*-
import urllib2, httplib, re
from bs4 import BeautifulSoup
from app import page_manager

metaClass = {
    'poslovni.hr': {"id": "__phlinews"},
    'seebiz.hr': {"id": "sc_content_okvirSREDINA_TOPTXOS"},
    'liderpress.hr': {"id": "__phlinews"},
    'tportal.hr': {"id": "article-column"},
    'ictbusiness.info': {"id": "__phlinews"},
    'banka.hr': {"class": "vijest_content"}
}


def getMetaDiv(meta):
    if meta is not None:
        if meta in metaClass:
            return metaClass.get(meta, "none")
        else:
            return "NaN"


def get_iframe_link(url):
    iframe_page_data = page_manager.get_web_page(url)

    if iframe_page_data is not None:
        soup = BeautifulSoup(iframe_page_data)

        iframeLink = soup.find('iframe')['src']

        return iframeLink


def get_iframe_content(link, meta):
    content = "NaN"
    divName = getMetaDiv(meta)
    if divName is not None and divName is not "NaN":
        main_page_data = page_manager.get_web_page(link)

        if main_page_data is not None:
            soup = BeautifulSoup(main_page_data)
            [s.extract() for s in soup('script')]
            content = soup.find('div', divName)

            if content is not None:
                content = content.text

    return content

